<template>
  <div class="distributeDia">
    <el-button type="text" size="small" @click="distribute">分配权限</el-button>
    <el-dialog title="分配权限" v-model="dialogFormVisible" :close-on-click-modal="false">
      <el-tree
        :data="powerData"
        show-checkbox
        node-key="id"
        ref="tree"
        highlight-current
        :props="defaultProps" accordion>
      </el-tree>
      <el-button @click="get" class="submit">确定</el-button>
    </el-dialog>
  </div>
</template>
<script>
  import {postLog} from 'api/common'
  export default{
      props:['id','powerList','name'],
      data(){
          return {
            dialogFormVisible:false,
            defaultProps: {
              children: 'children',
              label: 'label'
            }
          }
      },
      computed:{
        powerData(){
            return this.powerList;
        }
      },
      methods:{
//        获取该角色分配的权限
          distribute(){
            this.dialogFormVisible=true;
            this.$http.get('/api-web/v1/api/auth/menu/role/ids?roleId='+this.id)
              .then((response)=>{
                let data=response.body;
                console.log(data.result);
                this.$refs.tree.setCheckedKeys(data.result);
              },(err)=>{
                this.$alert('请求出错','提示');
              })
          },
//        分配权限
          get(){
            let powerArr=this.$refs.tree.getCheckedKeys();
            let json={
                menuIds:powerArr,
                roleId:this.id
            };
            this.$http.post('/api-web/v1/api/role/assignMenus',json)
              .then((response)=>{
                let data=response.body;
                if(data.resultCode=="200"){
                  this.dialogFormVisible=false;
                  postLog('SYSTEM_ROLE_DISTUBTE', this.id, this.name, json)

                }
              },(err)=>{
                this.$alert('请求出错','提示')
              })
          },
      }
  }
</script>
<style scoped>
</style>
